/*
 * @(#)FormElement.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.form.bean.FormElement;
import com.dcivision.form.bean.FormElementType;
import com.dcivision.form.bean.FormSelectionField;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.dao.MtmWfStepFormRecordDAObject;

/**
  FormElementDAObject.java

  This class is the data access bean for table "FORM_ELEMENT".

  @author      Jamin Wang
  @company     DCIVision Limited
  @creation date  18/1/2004
  @version     $Revision: 1.52.2.3 $
*/

public class FormElementDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.52.2.3 $";

  public static final String TABLE_NAME = "FORM_ELEMENT";

  public FormElementDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("ELEMENT_TYPE");
    this.vecDBColumn.add("DATA_TYPE");
    this.vecDBColumn.add("DEFAULT_VALUE");
    this.vecDBColumn.add("DEFAULT_STYLE");
    this.vecDBColumn.add("FIELD_NAME");
    this.vecDBColumn.add("IS_LABEL");
    this.vecDBColumn.add("FIELD_CAPTION");
    this.vecDBColumn.add("WIDTH");
    this.vecDBColumn.add("HEIGHT");
    this.vecDBColumn.add("EXPANDABLE");
    this.vecDBColumn.add("FONT_TYPE");
    this.vecDBColumn.add("FONT_SIZE");
    this.vecDBColumn.add("FONT_STYLE");
    this.vecDBColumn.add("FONT_COLOR");
    this.vecDBColumn.add("LABEL_POSITION");
    this.vecDBColumn.add("POSITION_ROW");
    this.vecDBColumn.add("POSITION_COLUMN");
    this.vecDBColumn.add("REQUIRED_FLAG");
    this.vecDBColumn.add("VALIDATION_RULE");
    this.vecDBColumn.add("UNIQUENESS");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN,A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A LEFT JOIN MTM_FORM_SECTION_FORM_ELEMENT B  ON A.ID = B.ELEMENT_ID ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
          
          FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);
          
          return(tmpFormElement);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
       if (searchForm.isSearchable()) {
        String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
       this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
          tmpFormElement.setRecordCount(totalNumOfRecord);
          tmpFormElement.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
    try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE,  A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
       while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));          
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormElement tmpFormElement = (FormElement)((FormElement)obj).clone();
    if (tmpFormElement.getRequiredFlag()==null){
      tmpFormElement.setRequiredFlag("N");
    }
    synchronized(dbConn) {
      try {

        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   FORM_ELEMENT(ID, ELEMENT_TYPE, DATA_TYPE, DEFAULT_VALUE, DEFAULT_STYLE, FIELD_NAME, IS_LABEL, FIELD_CAPTION, WIDTH, HEIGHT, EXPANDABLE, FONT_TYPE, FONT_SIZE, FONT_STYLE, FONT_COLOR, LABEL_POSITION, POSITION_ROW, POSITION_COLUMN, REQUIRED_FLAG, VALIDATION_RULE, UNIQUENESS, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpFormElement.getElementType());
        setPrepareStatement(preStat, 3, tmpFormElement.getDataType());
        setPrepareStatement(preStat, 4, tmpFormElement.getDefaultValue());
        setPrepareStatement(preStat, 5, tmpFormElement.getDefaultStyle());
        setPrepareStatement(preStat, 6, tmpFormElement.getFieldName());
        setPrepareStatement(preStat, 7, tmpFormElement.getIsLabel());
        setPrepareStatement(preStat, 8, tmpFormElement.getFieldCaption());
        setPrepareStatement(preStat, 9, tmpFormElement.getWidth());
        setPrepareStatement(preStat, 10, tmpFormElement.getHeight());
        setPrepareStatement(preStat, 11, tmpFormElement.getExpandable());
        setPrepareStatement(preStat, 12, tmpFormElement.getFontType());
        setPrepareStatement(preStat, 13, tmpFormElement.getFontSize());
        setPrepareStatement(preStat, 14, tmpFormElement.getFontStyle());
        setPrepareStatement(preStat, 15, tmpFormElement.getFontColor());
        setPrepareStatement(preStat, 16, tmpFormElement.getLabelPosition());
        setPrepareStatement(preStat, 17, tmpFormElement.getPositionRow());
        setPrepareStatement(preStat, 18, tmpFormElement.getPositionColumn());
        setPrepareStatement(preStat, 19, tmpFormElement.getRequiredFlag());
        setPrepareStatement(preStat, 20, tmpFormElement.getValidationRule());
        setPrepareStatement(preStat, 21, tmpFormElement.getUniqueness());
        setPrepareStatement(preStat, 22, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 23, new Integer(0));
        setPrepareStatement(preStat, 24, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 25, currTime);
        setPrepareStatement(preStat, 26, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 27, currTime);
        preStat.executeUpdate();
        if (FormElementType.ELEMENT_IMAGE.equals(tmpFormElement.getElementType())){
          sqlStat=new StringBuffer("");
          if (tmpFormElement.getFileName()==null){
            tmpFormElement.setFileName("");
          }
          sqlStat.append("INSERT ");
          sqlStat.append("INTO   FORM_ATTACH (ID,FILE_NAME,RECORD_STATUS,UPDATE_COUNT,CREATOR_ID,CREATE_DATE,UPDATER_ID,UPDATE_DATE) ");
          sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?) ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, nextID);
          setPrepareStatement(preStat, 2, tmpFormElement.getFileName());
          setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
          setPrepareStatement(preStat, 4, new Integer(0));
          setPrepareStatement(preStat, 5,sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 6,currTime);
          setPrepareStatement(preStat, 7,sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 8,currTime);
          preStat.executeUpdate();
        }
        tmpFormElement.setID(nextID);
        tmpFormElement.setCreatorID(sessionContainer.getUserRecordID());
        tmpFormElement.setCreateDate(currTime);
        tmpFormElement.setUpdaterID(sessionContainer.getUserRecordID());
        tmpFormElement.setUpdateDate(currTime);
        tmpFormElement.setUpdateCount(new Integer(0));
        tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
        tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
       return(tmpFormElement);

      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    java.sql.ResultSet sqlRsult =null;
    StringBuffer sqlStat = new StringBuffer();
    FormElement tmpFormElement = (FormElement)((FormElement)obj).clone();
    synchronized(dbConn) {
      try {
          int updateCnt = 0;
          Timestamp currTime = Utility.getCurrentTimestamp();
          sqlStat.append("UPDATE FORM_ELEMENT ");
          sqlStat.append("SET  ELEMENT_TYPE=?, DATA_TYPE=?, DEFAULT_VALUE=?, DEFAULT_STYLE=?, FIELD_NAME=?, IS_LABEL=?, FIELD_CAPTION=?, WIDTH=?, HEIGHT=?, EXPANDABLE=?, FONT_TYPE=?, FONT_SIZE=?, FONT_STYLE=?, FONT_COLOR=?, LABEL_POSITION=?, POSITION_ROW=?, POSITION_COLUMN=?, REQUIRED_FLAG=?, VALIDATION_RULE=?, UNIQUENESS=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
          sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpFormElement.getElementType());
          setPrepareStatement(preStat, 2, tmpFormElement.getDataType());
          setPrepareStatement(preStat, 3, tmpFormElement.getDefaultValue());
          setPrepareStatement(preStat, 4, tmpFormElement.getDefaultStyle());
          setPrepareStatement(preStat, 5, tmpFormElement.getFieldName());
          setPrepareStatement(preStat, 6, tmpFormElement.getIsLabel());
          setPrepareStatement(preStat, 7, tmpFormElement.getFieldCaption());
          setPrepareStatement(preStat, 8, tmpFormElement.getWidth());
          setPrepareStatement(preStat, 9, tmpFormElement.getHeight());
          setPrepareStatement(preStat, 10, tmpFormElement.getExpandable());
          setPrepareStatement(preStat, 11, tmpFormElement.getFontType());
          setPrepareStatement(preStat, 12, tmpFormElement.getFontSize());
          setPrepareStatement(preStat, 13, tmpFormElement.getFontStyle());
          setPrepareStatement(preStat, 14, tmpFormElement.getFontColor());
          setPrepareStatement(preStat, 15, tmpFormElement.getLabelPosition());
          setPrepareStatement(preStat, 16, tmpFormElement.getPositionRow());
          setPrepareStatement(preStat, 17, tmpFormElement.getPositionColumn());
          setPrepareStatement(preStat, 18, tmpFormElement.getRequiredFlag());
          setPrepareStatement(preStat, 19, tmpFormElement.getValidationRule());
          setPrepareStatement(preStat, 20, tmpFormElement.getUniqueness());
          setPrepareStatement(preStat, 21, new Integer(tmpFormElement.getUpdateCount().intValue() + 1));
          setPrepareStatement(preStat, 22, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 23, currTime);
          setPrepareStatement(preStat, 24, tmpFormElement.getID());
          setPrepareStatement(preStat, 25, tmpFormElement.getUpdateCount());
          updateCnt = preStat.executeUpdate();
          if (!tmpFormElement.getElementType().equals(FormElementType.ELEMENT_IMAGE)) {
            sqlStat=new StringBuffer("");
            sqlStat.append("DELETE ");
            sqlStat.append("FROM   FORM_ATTACH ");
            sqlStat.append("WHERE  ID=?  AND RECORD_STATUS = ? ");
            preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            setPrepareStatement(preStat, 1, tmpFormElement.getID());
            setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
            preStat.executeUpdate();
          }else{
            if (tmpFormElement.getFileName()!=null ) {
              sqlStat=new StringBuffer("");
              sqlStat.append("SELECT * ");
              sqlStat.append("FROM FORM_ATTACH  ");
              sqlStat.append("WHERE ID= ? AND RECORD_STATUS = ?");
              preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
              setPrepareStatement(preStat, 1, tmpFormElement.getID());
              setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
              sqlRsult = preStat.executeQuery();
              if (!sqlRsult.next()){
                sqlStat=new StringBuffer("");
                sqlStat.append("INSERT ");
                sqlStat.append("INTO   FORM_ATTACH (ID,FILE_NAME,RECORD_STATUS,UPDATE_COUNT,CREATOR_ID,CREATE_DATE,UPDATER_ID,UPDATE_DATE) ");
                sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?) ");
                preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                setPrepareStatement(preStat, 1,tmpFormElement.getID());
                setPrepareStatement(preStat, 2,tmpFormElement.getFileName());
                setPrepareStatement(preStat, 3,GlobalConstant.RECORD_STATUS_ACTIVE);
                setPrepareStatement(preStat, 4,new Integer(tmpFormElement.getUpdateCount().intValue() + 1));
                setPrepareStatement(preStat, 5,sessionContainer.getUserRecordID());
                setPrepareStatement(preStat, 6,currTime);
                setPrepareStatement(preStat, 7,sessionContainer.getUserRecordID());
                setPrepareStatement(preStat, 8,currTime);
                preStat.executeUpdate();
             }else  {
               if (tmpFormElement.getFileName()!=null)  {
                 if (!tmpFormElement.getFileName().equals(""))  {
                   sqlStat=new StringBuffer("");
                   sqlStat.append("UPDATE ");
                   sqlStat.append(" FORM_ATTACH  SET FILE_NAME=?,UPDATE_COUNT=?,UPDATER_ID=?,UPDATE_DATE=? ");
                   sqlStat.append(" WHERE ID=?");
                   preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                   setPrepareStatement(preStat, 5,tmpFormElement.getID());
                   setPrepareStatement(preStat, 1,tmpFormElement.getFileName());
                   setPrepareStatement(preStat, 2,new Integer(tmpFormElement.getUpdateCount().intValue() + 1));
                   setPrepareStatement(preStat, 3,sessionContainer.getUserRecordID());
                   setPrepareStatement(preStat, 4,currTime);
                   preStat.executeUpdate();
                 }
               }
             }
           }
          }
          if (updateCnt == 0) {
            throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
          } else {
            tmpFormElement.setUpdaterID(sessionContainer.getUserRecordID());
            tmpFormElement.setUpdateDate(currTime);
            tmpFormElement.setUpdateCount(new Integer(tmpFormElement.getUpdateCount().intValue() + 1));
            tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
            tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
            return(tmpFormElement);
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
        } finally {
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }


  public synchronized void  updateByFile(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    java.sql.ResultSet sqlRsult =null;
    StringBuffer sqlStat = new StringBuffer();
    FormElement tmpFormElement = (FormElement)((FormElement)obj).clone();
     synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE FORM_ELEMENT ");
        sqlStat.append("SET   ");
       if (tmpFormElement.getDefaultValue()!=null)
        sqlStat.append(" DEFAULT_VALUE='"+tmpFormElement.getDefaultValue()+"',");
       if (tmpFormElement.getFieldName()!=null)
        sqlStat.append(" FIELD_NAME='"+tmpFormElement.getFieldName()+"',");
       if (tmpFormElement.getFontType()!=null)
       sqlStat.append("  FONT_TYPE='"+tmpFormElement.getFontType()+"', ");
     if (tmpFormElement.getFontSize()!=null)
       sqlStat.append("  FONT_SIZE="+tmpFormElement.getFontSize()+", ");
      if (tmpFormElement.getFontStyle()!=null)
       sqlStat.append("  FONT_STYLE='"+tmpFormElement.getFontStyle()+"', ");
      if (tmpFormElement.getFontColor()!=null)
       sqlStat.append("  FONT_COLOR='"+tmpFormElement.getFontColor()+"' , ");
       if (tmpFormElement.getRequiredFlag()!=null)
        sqlStat.append(" REQUIRED_FLAG='"+tmpFormElement.getRequiredFlag()+"',");
        sqlStat.append(" UPDATE_COUNT=UPDATE_COUNT+1  WHERE  ID="+tmpFormElement.getID());

       if (tmpFormElement.getFieldName()!=null){
        preStat = dbConn.prepareStatement(sqlStat.toString());
        preStat.executeUpdate();
        dbConn.commit();
       }

      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }



  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormElement tmpFormElement = (FormElement)((FormElement)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        int updateAttach=1;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   FORM_ELEMENT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpFormElement.getID());
        setPrepareStatement(preStat, 2, tmpFormElement.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        
        if (FormElementType.ELEMENT_IMAGE.equals(tmpFormElement.getElementType())){
          sqlStat=new StringBuffer("");
          sqlStat.append("DELETE ");
          sqlStat.append("FROM   FORM_ATTACH ");
          sqlStat.append("WHERE  ID=?   ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpFormElement.getID());
          updateAttach = preStat.executeUpdate();
        }
        
        if (updateCnt == 0||updateAttach == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpFormElement);
        }

      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


  public int getNexID() throws ApplicationException {
      PreparedStatement preStat = null;
      StringBuffer sqlStat = new StringBuffer();
      java.sql.ResultSet sqlRsult=null;
      synchronized(dbConn) {
        try {
           sqlStat.append("SELECT max(ID)+1 AS NEXID ");
           sqlStat.append("FROM   FORM_ELEMENT ");
          // sqlStat.append("WHERE   RECORD_STATUS='A' ");
           preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

           sqlRsult = preStat.executeQuery();
         if (sqlRsult.next())
          return sqlRsult.getInt(1);
        else
          return 0;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
        } finally {
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }


  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    FormElement tmpFormElement = (FormElement)this.oldValue;

    if (tmpFormElement != null) {
      oldValues.add(toAuditTrailValue(tmpFormElement.getID()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getElementType()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getDataType()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getDefaultValue()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getDefaultStyle()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getFieldName()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getIsLabel()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getFieldCaption()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getWidth()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getHeight()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getExpandable()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getFontType()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getFontSize()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getFontStyle()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getFontColor()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getLabelPosition()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getPositionRow()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getPositionColumn()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getRequiredFlag()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getValidationRule()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getUniqueness()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpFormElement.getUpdateDate()));
    }

    tmpFormElement = (FormElement)obj;
    if (tmpFormElement != null) {
      newValues.add(toAuditTrailValue(tmpFormElement.getID()));
      newValues.add(toAuditTrailValue(tmpFormElement.getElementType()));
      newValues.add(toAuditTrailValue(tmpFormElement.getDataType()));
      newValues.add(toAuditTrailValue(tmpFormElement.getDefaultValue()));
      newValues.add(toAuditTrailValue(tmpFormElement.getDefaultStyle()));
      newValues.add(toAuditTrailValue(tmpFormElement.getFieldName()));
      newValues.add(toAuditTrailValue(tmpFormElement.getIsLabel()));
      newValues.add(toAuditTrailValue(tmpFormElement.getFieldCaption()));
      newValues.add(toAuditTrailValue(tmpFormElement.getWidth()));
      newValues.add(toAuditTrailValue(tmpFormElement.getHeight()));
      newValues.add(toAuditTrailValue(tmpFormElement.getExpandable()));
      newValues.add(toAuditTrailValue(tmpFormElement.getFontType()));
      newValues.add(toAuditTrailValue(tmpFormElement.getFontSize()));
      newValues.add(toAuditTrailValue(tmpFormElement.getFontStyle()));
      newValues.add(toAuditTrailValue(tmpFormElement.getFontColor()));
      newValues.add(toAuditTrailValue(tmpFormElement.getLabelPosition()));
      newValues.add(toAuditTrailValue(tmpFormElement.getPositionRow()));
      newValues.add(toAuditTrailValue(tmpFormElement.getPositionColumn()));
      newValues.add(toAuditTrailValue(tmpFormElement.getRequiredFlag()));
      newValues.add(toAuditTrailValue(tmpFormElement.getValidationRule()));
      newValues.add(toAuditTrailValue(tmpFormElement.getUniqueness()));
      newValues.add(toAuditTrailValue(tmpFormElement.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpFormElement.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpFormElement.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpFormElement.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpFormElement.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpFormElement.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public synchronized List getListByFormSectionID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
          
          FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);
          
          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getDetailListByFormSectionID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);

          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  
  public synchronized List getDetailListByFormRecordIDPageIDSectionIDStepID(Integer formRecordID, Integer formPageID, Integer formSectionID, Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    String permission = "";
    
    synchronized(dbConn) {
      try {      
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);

        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ "); 
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formSectionID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);
          if (workflowStepID.intValue()>0){
            permission = mtmWfStepFormRecordDAO.getFormElementPermissionByWorkflowStepIDFormRecordIDFormPageIDFormSectionIDFormElementID(workflowStepID, formRecordID, formPageID, formSectionID, tmpFormElement.getID());
          }
          tmpFormElement.setPermission(permission);   
          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getSearchListByFormSectionID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("AND A.ELEMENT_TYPE<> '"+FormElementType.ELEMENT_IMAGE+"' ");
        sqlStat.append("AND A.ELEMENT_TYPE<> '"+FormElementType.ELEMENT_LABEL+"' ");
        sqlStat.append("AND (A.IS_LABEL IS NULL OR A.IS_LABEL <> 'Y')");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);

          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getTextBoxSearchListByFormSectionID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("AND (A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_TEXT+"' ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_PASSWORD+"' ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_TEXT_AREA+"' ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_CURRENT_LOGIN+"'  ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_TIME_PICKER+"'  ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_SIGNATURE+"' ) ");
        sqlStat.append("AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          //FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          //FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          //tmpFormElement.setFormSelectionField(formSelectionField);

          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getDatePickerSearchListByFormSectionID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("AND (A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_CURRENT_DATE+"' ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_DATE_PICKER+"' ) ");
        sqlStat.append("AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          //FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          //FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          //tmpFormElement.setFormSelectionField(formSelectionField);

          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getSelectBoxSearchListByFormSectionID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("AND (A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_RADIO_BUTTON+"' ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_SELECTION_BOX+"' ");
        sqlStat.append("OR A.ELEMENT_TYPE= '"+FormElementType.ELEMENT_CHECK_BOX+"') ");
        sqlStat.append("AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);

          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * get the permission for elements in the section.
   * @param formRecordID
   * @param formPageID
   * @param formSectionID
   * @param workflowStepID
   * @return
   * @throws ApplicationException
   */
  public synchronized boolean getSectionElementsPermissionByFormRecordIDPageIDSectionIDStepID(Integer formRecordID, Integer formPageID, Integer formSectionID, Integer workflowStepID, boolean isViewUserStep) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    String permission = "";
    Integer formElementID = null;
    boolean hasPermission = false; 
    if ("null".equals(workflowStepID) || workflowStepID.intValue()<1){
      return true;   
    }    
    synchronized(dbConn) {
      try {      
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
 
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ "); 
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formSectionID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          formElementID = getResultSetInteger(rs, "ID");
          permission = mtmWfStepFormRecordDAO.getFormElementPermissionByWorkflowStepIDFormRecordIDFormPageIDFormSectionIDFormElementID(workflowStepID, formRecordID, formPageID, formSectionID, formElementID);
          if (!Utility.isEmpty(permission) && !"null".equals(permission)){
            if (!isViewUserStep){
              hasPermission = true; 
              break;
            }else if (isViewUserStep && !WorkflowStep.FORM_PROCESS_PERMISSION_READ.equals(permission)){
              hasPermission = true; 
              break;
            }                    
          }              
        }
        return hasPermission;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getIsLabelElementListByFormSectionID(Integer formSectionID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    String permission = "";
    
    synchronized(dbConn) {
      try {      
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);

        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE A.RECORD_STATUS = B.RECORD_STATUS AND A.IS_LABEL = 'Y' ");
        sqlStat.append("AND A.ID = B.ELEMENT_ID AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ "); 
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formSectionID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));
          
          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
    
  public synchronized List getRelatedItemElementListByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.ELEMENT_TYPE, A.DATA_TYPE, A.DEFAULT_VALUE, A.DEFAULT_STYLE, A.FIELD_NAME, A.IS_LABEL, A.FIELD_CAPTION, A.WIDTH, A.HEIGHT, A.EXPANDABLE, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.LABEL_POSITION, A.POSITION_ROW, A.POSITION_COLUMN, A.REQUIRED_FLAG, A.VALIDATION_RULE, A.UNIQUENESS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   FORM_ELEMENT A, MTM_FORM_SECTION_FORM_ELEMENT B ");
        sqlStat.append("WHERE  A.ID = B.ELEMENT_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND B.SECTION_ID = ? AND A.RECORD_STATUS = ? AND A.IS_LABEL <> 'Y' ");
        sqlStat.append("AND A.REQUIRED_FLAG = 'Y' ");
        sqlStat.append("ORDER BY B.ELEMENT_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormElement tmpFormElement = new FormElement();
          tmpFormElement.setID(getResultSetInteger(rs, "ID"));
          tmpFormElement.setElementType(getResultSetString(rs, "ELEMENT_TYPE"));
          tmpFormElement.setDataType(getResultSetString(rs, "DATA_TYPE"));
          tmpFormElement.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpFormElement.setDefaultStyle(getResultSetInteger(rs, "DEFAULT_STYLE"));
          tmpFormElement.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpFormElement.setIsLabel(getResultSetString(rs, "IS_LABEL"));
          tmpFormElement.setFieldCaption(getResultSetString(rs, "FIELD_CAPTION"));
          tmpFormElement.setWidth(getResultSetInteger(rs, "WIDTH"));
          tmpFormElement.setHeight(getResultSetInteger(rs, "HEIGHT"));
          tmpFormElement.setExpandable(getResultSetInteger(rs, "EXPANDABLE"));
          tmpFormElement.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpFormElement.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpFormElement.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpFormElement.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpFormElement.setLabelPosition(getResultSetString(rs, "LABEL_POSITION"));
          tmpFormElement.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpFormElement.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpFormElement.setRequiredFlag(getResultSetString(rs, "REQUIRED_FLAG"));
          tmpFormElement.setValidationRule(getResultSetString(rs, "VALIDATION_RULE"));
          tmpFormElement.setUniqueness(getResultSetString(rs, "UNIQUENESS"));
          tmpFormElement.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormElement.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormElement.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormElement.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormElement.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormElement.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormElement.setCreatorName(UserInfoFactory.getUserFullName(tmpFormElement.getCreatorID()));
          tmpFormElement.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormElement.getUpdaterID()));

          FormSelectionFieldDAObject formSelectionFieldDAO = new FormSelectionFieldDAObject(sessionContainer, dbConn);
          FormSelectionField formSelectionField = (FormSelectionField)formSelectionFieldDAO.getDetailByElementID(tmpFormElement.getID());
          tmpFormElement.setFormSelectionField(formSelectionField);

          result.add(tmpFormElement);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

}
